/* eslint-disable no-use-before-define */
import {
  saveTableDetail,
  asyncSetInterval,
} from "../../lib/zeroReportUtils.js";

let css =
  ".hidden{display:none;}.sb_table{display:inline-table;table-layout:fixed;margin-right:5px;border-collapse:collapse;}.sb_table tr{text-align:center;}.sb_table tr.txt-l,.sb_table td.txt-l{text-align:left;}.sb_table tr.txt-r,.sb_table td.txt-r{text-align:right;}.sb_table > tbody td,.sb_table > tbody tr{border:1px solid #dfdfdf;font-weight:400;line-height:32px;overflow:hidden;}.sb_table > tbody input{background:0 0;color:#333;font-size:14px;width:100%;height:100%;line-height:32px;padding:0;border:0;outline:0;text-align:center;box-sizing:border-box;vertical-align:top;}";

let asyncTableStart = async () => {
  let trs = $("#content-table > tbody tr");
  let biTian = Array.from(trs).filter((item) =>
    $(item).find("td.table-needed").text().includes("必填")
  );
  let flzl1 = biTian.find((item) =>
    $(item).find("td.table-name").text().includes("申报表附列资料一")
  );
  let flzl2 = biTian.find((item) =>
    $(item).find("td.table-name").text().includes("申报表附列资料二")
  );
  let flzl3 = biTian.find((item) =>
    $(item).find("td.table-name").text().includes("申报表附列资料三")
  );
  let sbb = biTian.find((item) =>
    $(item)
      .find("td.table-name")
      .text()
      .includes("增值税纳税申报表（一般纳税人适用")
  );
  let fjssb = biTian.find((item) =>
    $(item).find("td.table-name").text().includes("增值税一般纳税人附加税申报")
  );
  await asyncZzsYbnsrTablType(flzl1);
  await asyncZzsYbnsrTablType(flzl2);
  await asyncZzsYbnsrTablType(flzl3);
  await asyncZzsYbnsrTablType(sbb);
  await asyncZzsYbnsrTablType(fjssb);
  return true;
};
let asyncZzsYbnsrTablType = async (flzl1) => {
  if (flzl1) {
    return new Promise(async (resolve) => {
      setTimeout(async () => {
        flzl1.querySelector(".table-operation > .sb_view").click();
        let result = await asyncZzsYbnsrTabl();
        resolve(result);
      }, 1000);
    });
  }
  return false;
};
let asyncZzsYbnsrTabl = async () => {
  return asyncSetInterval(() => {
    let iframe = document.querySelector(
      ".mini-panel.mini-window div.mini-panel-viewport > div.mini-panel-body > iframe"
    );
    if (iframe) {
      return iframe;
    }
  }, 1000)
    .then((iframe) => {
      return new Promise((resolve) => {
        let met = iframe.getAttribute("onload");
        iframe.onload = () => {
          eval(`window.${met}`);
          resolve(iframe);
        };
      });
    })
    .then((iframe) => {
      return asyncSetInterval(() => {
        let miniMask = document.querySelector(".mini-mask");
        if (!miniMask) {
          return iframe;
        }
      }, 1000);
    })
    .then((iframe) => {
      let html = iframe.contentDocument.querySelector(".table-box > table");
      let title = "";
      try {
        title = html.getAttribute("sb_title");
      } catch {
        title = html.querySelector(".table-title").innerText;
      }
      let tableContent = {
        declareType: "增值税小规模纳税人申报（季报）",
        title: title,
        html: html.outerHTML,
        css,
      };
      // console.log(tableContent);
      saveTableDetail(tableContent);
      let btns = $(".mini-tools .mini-tools-close");
      Array.from(btns).forEach((item) => {
        item.click();
      });
      return true;
    });
};

let saveTableDetailXgmnsr = async ({ zzsnsssb, flzl, jzmmb, fjs }) => {
  console.log({ zzsnsssb, flzl, jzmmb, fjs });
  await asyncTableDetailXgmnsrDetial(zzsnsssb, "#table_001");
  await asyncTableDetailXgmnsrDetial(flzl, "#table_006");
  await asyncTableDetailXgmnsrDetial(jzmmb, "#table_008");
  await asyncTableDetailXgmnsrDetial(fjs, "#table_300");
  return true;
};


let startQysdsTableDetail = async ({tabs, tables}) => {
  console.log(tables);
  console.log(tabs);
  return asyncSetInterval(() => {
    let tab = Array.from(tabs).find(item => $(item).text().includes('A200000中华人民共和国企业所得税月'));
    let table = Array.from(tables).find(item => $(item).attr('sb_title').includes('A200000中华人民共和国企业所得税月'));
    console.log(tab);
    console.log(table);
  });
}

async function asyncTableDetailXgmnsrDetial(zzsnsssb, id) {
  if (zzsnsssb) {
    zzsnsssb.click();
    return asyncSetInterval(() => {
      let table = document.querySelector(id);
      Array.from($(table).find("input")).forEach((item) => {
        $(item).attr("disabled", "true");
      });
      let tableContent = {
        declareType: "增值税小规模纳税人申报",
        title: table.getAttribute("sb_title"),
        html: table.outerHTML,
        css,
      };
      console.log(tableContent);
      saveTableDetail(tableContent);
      return true;
    }, 1000);
  }
}

export { asyncTableStart, saveTableDetailXgmnsr, startQysdsTableDetail };
